<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="获奖名称" prop="awardName">
        <el-input
          v-model="queryParams.awardName"
          placeholder="请输入获奖名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖日期">
        <el-date-picker
          v-model="daterangeAwardDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="获奖等级" prop="awardGrade">
        <el-select v-model="queryParams.awardGrade" placeholder="请选择获奖等级" clearable>
          <el-option
            v-for="dict in dict.type.work_award_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:award:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:award:remove']"
        >删除</el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="awardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="获奖名称" align="center" prop="awardName" >
        <template slot-scope="scope">
          <el-link style="color: #371ce3" @click="clickDetail(scope.row)">{{scope.row.awardName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="获奖日期" align="center" prop="awardDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.awardDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获奖等级" align="center" prop="awardGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_award_grade" :value="scope.row.awardGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['work:award:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:award:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="获奖详情" :visible.sync="openDetail" width="1200px" append-to-body>
      <award-detail :id="awardId" v-if="openDetail">

      </award-detail>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改获奖对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <AddAward @handleClose="handleClose" v-if="open" :query-params="form" :type="openClick"></AddAward>
    </el-dialog>
  </div>
</template>

<script>
import { listAward, getAward, delAward, addAward, updateAward } from "@/api/work/award";
import {listAwardWinners,delAwardWinners} from "@/api/work/awardWinners";
import AddAward from "@/views/work/award/AddAword/index.vue";
import AwardDetail from "@/views/work/award/Detail/index.vue";
import {getUserProfile} from "@/api/system/user";


export default {
  name: "Award",
  components: {AwardDetail, AddAward},
  dicts: ['author_type', 'unit_ranking', 'work_award_grade', 'work_award_level', 'sys_yes_no', 'sys_user_sex', 'school_audit_status', 'subject_category', 'faculty_audit_status', 'professional_title'],
  props:{
    queryParamss:{
      type:Object,
      required: true,
      default: () => ({
        pageNum: 1,
        pageSize: 10,
        awardName: null,
        awardDate: null,
        awardGrade: null,
        fistWinner: null,
        resultName: null,
        winnersNumber: null,
        licenceissuingAuthority: null,
        awardLevel: null,
        pizeSpecies: null,
        completionUnit: null,
        affiliatedUnit: null,
        isCallForPapers: null,
        participationgUnitsNum: null,
        unitRanking: null,
        awardCode: null,
        subjectCategoryId: null,
        fistSubject: null,
        projectSource: null,
        resultForm: null,
        awardType: null,
        filesUrl: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId: null
      })
    }
  },
  data() {
    return {
      //获奖详情id
      awardId:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWorkAwardWinners: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 获奖表格数据
      awardList: [],
      // 获奖完成人表格数据
      workAwardWinnersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail:false,
      openClick:true,
      // 查询参数
      queryParams:this.queryParams,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fistSubject: [
          { required: true, message: "一级学科不能为空", trigger: "blur" }
        ],
        projectSource: [
          { required: true, message: "项目来源不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams=this.queryParamss
    this.getList();
  },
  methods: {
    /** 查询获奖列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAwardDate && '' != this.daterangeAwardDate) {
        this.queryParams.params["beginAwardDate"] = this.daterangeAwardDate[0];
        this.queryParams.params["endAwardDate"] = this.daterangeAwardDate[1];
      }

      //获取当前登录用户id并添加筛选
      getUserProfile().then(res => {

        this.queryParams.userId = res.data.userId;
      listAward(this.queryParams).then(response => {
        this.awardList = response.rows;
        this.total = response.total;
        console.log(this.awardList)
        console.log(res)
        this.loading = false;

      });
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        awardName: null,
        awardDate: null,
        awardGrade: null,
        fistWinner: null,
        resultName: null,
        winnersNumber: null,
        licenceissuingAuthority: null,
        awardLevel: null,
        pizeSpecies: null,
        completionUnit: null,
        affiliatedUnit: null,
        isCallForPapers: null,
        participationgUnitsNum: null,
        unitRanking: null,
        awardCode: null,
        subjectCategoryId: null,
        fistSubject: null,
        projectSource: null,
        resultForm: null,
        awardType: null,
        filesUrl: null,
        remark: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId: null
      };
      this.workAwardWinnersList = [];
      this.awardId=null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAwardDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.openClick = true;
      this.reset();
      this.open = true;
      this.title = "添加获奖";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAward(id).then(response => {
        this.form = response.data;
        this.workAwardWinnersList = response.data.workAwardWinnersList;
        this.openClick = false;
        this.open = true;
        this.title = "修改获奖";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.workAwardWinnersList = this.workAwardWinnersList;
          if (this.form.id != null) {
            updateAward(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAward(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除获奖编号为"' + ids + '"的数据项？').then(function () {
        return delAward(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /**提交取消按钮事件*/
    handleClose(){
      this.getList()
      this.open = false;
    },

    // 详情链接点击事件
    clickDetail(row){
      this.openDetail = true;
      this.awardId = row.id;
    },
    closeDialog() {
      this.reset();
    },
    /** 获奖完成人序号 */
    rowWorkAwardWinnersIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 获奖完成人添加按钮操作 */
    handleAddWorkAwardWinners() {
      let obj = {};
      obj.signatureOrder = "";
      obj.authorType = "";
      obj.parsonCharge = "";
      obj.gender = "";
      obj.educationBackgorund = "";
      obj.affiliatedUnit = "";
      obj.contributionRate = "";
      obj.professionalTitle = "";
      this.workAwardWinnersList.push(obj);
    },
    /** 获奖完成人删除按钮操作 */
    handleDeleteWorkAwardWinners() {
      if (this.checkedWorkAwardWinners.length == 0) {
        this.$modal.msgError("请先选择要删除的获奖完成人数据");
      } else {
        const workAwardWinnersList = this.workAwardWinnersList;
        const checkedWorkAwardWinners = this.checkedWorkAwardWinners;
        this.workAwardWinnersList = workAwardWinnersList.filter(function (item) {
          return checkedWorkAwardWinners.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWorkAwardWinnersSelectionChange(selection) {
      this.checkedWorkAwardWinners = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/award/export', {
        ...this.queryParams
      }, `award_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
